<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        span {
            display: inline-block;
            width: 30px;
            height: 30px;
            border: 1px solid #000;
        }

        .checked {
            background-color: #f00;
        }
    </style>
</head>

<body>

    <span></span>
    <span></span>
    <span></span>
    <span></span>


    <script>


        var oSpans = document.querySelectorAll('span');

        oSpans.forEach(v => {
            v.onclick = function () {
                if (this.classList.contains('checked')) {
                    this.classList.remove('checked')
                } else {
                    this.classList.add('checked')
                }
            }
        })

    </script>

</body>

</html>